package egovframework.npqs.pqi.mobile;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;

public class ActivityTest extends Activity {
	private ImageView photo_view;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		
		init();
	}
	
	public void init(){
		photo_view = (ImageView)findViewById(R.id.photo_view);
		
		new yourTask().execute();
		
	}
	
	private class yourTask extends AsyncTask<Integer, Void, Bitmap>{
		//String imgStr = "http://10.110.128.135:8086/upload/fe/pqi/mobile/2015/01/12/2015000007_01.jpg";
		String imgStr = "http://152.99.157.173:8086/upload/fe/pqi/mobile/2015/01/12/2015000007_01.jpg";

		InputStream is = null;
		BufferedInputStream bis = null;
		Bitmap bit = null;
		Drawable d = null;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Bitmap doInBackground(Integer... params) {
			try {
				System.out.println(imgStr);
				URL imageUrl = new URL(imgStr);
				URLConnection conn = imageUrl.openConnection();
				conn.connect();
				
				is = conn.getInputStream();
				bis = new BufferedInputStream(is);
				bit = BitmapFactory.decodeStream(bis);
				
				bis.close();
				is.close();
				
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return bit;
		}
		
		@Override
		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
			d = new BitmapDrawable(result);
			//photo_view.setImageBitmap(bit);
			photo_view.setImageDrawable(d);
		}
		
	}

}
